const app = getApp()
var request = require('../../utils/request.js')

Page({
    data: {
        newList: [
            {
                fid: '1',
                type: '表白',
                title: '',
                contact: {
                    qq: '',
                    wechat: '',
                    tel: '',
                    mail: ''
                },
                content: '',
                pic: [],
                time: '2021-08-04',
                animation:false
            }
        ]
    },
    onShow(){
        this.getNewList()
    },
    animation(index) {
        var that=this
        var animation="newList["+index+"].animation"
        that.setData({
            [animation]: true
        })
        setTimeout(function () {
            that.data.newList.splice(index,1)
            that.setData({
                newList:that.data.newList
            })
        }, 500)
    },
    getNewList(){
        let that = this
        request.GET('/api/list/checklist').then(
            function(res){
                let list = res.data
                console.log(list)
                list = list.map((item)=>{
                    // 处理联系方式
                    let tContact = item.contact
                    item.contact = JSON.parse(tContact)
                    item.pic = item.pic.map((item1)=>{
                        // 图片处理
                        return app.globalData.baseUrl + item1
                    })

                    item.thumb = item.thumb.map((item2)=>{
                        // 图片处理
                        return app.globalData.baseUrl + item2
                    })
                    return item
                })
                that.setData({
                    newList: list
                })
            }
        )
    },
    agree(e) {
        qq.showLoading()
        let that = this
        let fid = e.currentTarget.dataset.fid
        console.log(fid)
        request.POST('/api/list/check',{
            'fid': fid,
            'result': true    
        }).then(
            function(res){
                if(res.data.success){
                    that.animation(e.currentTarget.dataset.index)
                }else{
                    wx.showModal({
                        content:'此表单后台处理出错，请联系开发者进行反馈'
                    })
                }
            }
        )
    },
    refuse(e) {
        // qq.showLoading()
        let that = this
        let fid = e.currentTarget.dataset.fid
        console.log(fid)
        that.animation(e.currentTarget.dataset.index)//由于拒绝不会报错，为提高效率，不等res直接让卡片消失
        request.POST('/api/list/check',{
            'fid': fid,
            'result': false    
        }).then(
            // function(res){
            //     if(res.data.success){
            //         that.animation(e.currentTarget.dataset.index)
            //     }
            // }
        )
    },
    detail(e) {
        let fid = e.currentTarget.dataset.fid
        console.log(fid)
    },
    ViewImage(e) {
        console.log(e.currentTarget.dataset)
        console.log(this.data.newList)
        qq.previewImage({
            urls: this.data.newList[e.currentTarget.dataset.index].pic,
            current: e.currentTarget.dataset.url
        });
    },

    onShareAppMessage: function () {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        })
    },
})